rpm -qa | grep expect
yum -y install expect


-----
apt-get -y update
apt-get -y install expect

#  回车打印显示 pressed enter 字符
#  单一分支模式的语法
expect -c 'expect "\n" {send "pressed enter\n"}' 
# 匹配到 hi 后,会输出"you said hi"，并换行
expect "hi" { send "You said hi\n" }     
# 多分支模式的语法
# 匹配 hi, hehe, bye 中的任意字符串时, 发送相应字符串
expect "hi" { send "You said hi\n" } \ "hehe" { send “Hehe yourself\n" } \ "bye" { send "Goodbye\n" } 



expect -c 'spawn ssh root@192.168.56.101 
expect {
    "yes/no" {send "yes\n"; exp_continue} 
    "password" {send "root\n"} 
}
expect eof'


----
#!/bin/bash
ip=$1
user=$2
password=$2
expect <<EOF
    set timeout 10
    spawn ssh $user@$ip
    expect {
        "yes/no" {send "yes\n"; exp_continue}
        "password" {send "$password\n}
    }
    expect "]#" {send "ip a\n"}
EOF


